package com.zc.mychart.chart;

import android.graphics.Matrix;
import android.view.MotionEvent;
import android.view.View;

import com.github.mikephil.charting.charts.BarLineChartBase;
import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet;
import com.github.mikephil.charting.listener.BarLineChartTouchListener;

/**
 * Created by Mr on 2018/4/25.
 */

public class MyBarLineChartTouchListener extends BarLineChartTouchListener {
    /**
     * Constructor with initialization parameters.
     *
     * @param chart               instance of the chart
     * @param touchMatrix         the touch-matrix of the chart
     * @param dragTriggerDistance the minimum movement distance that will be interpreted as a "drag" gesture in dp (3dp equals
     */
    public MyBarLineChartTouchListener(BarLineChartBase<? extends BarLineScatterCandleBubbleData<? extends IBarLineScatterCandleBubbleDataSet<? extends Entry>>> chart, Matrix touchMatrix, float dragTriggerDistance) {
        super(chart, touchMatrix, dragTriggerDistance);
    }


    @Override
    public boolean onTouch(View v, MotionEvent e) {
        mChart.setDragEnabled(!mChart.valuesToHighlight());
        if (mChart.valuesToHighlight()) {
            switch (e.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    Highlight h = mChart.getHighlightByTouchPoint(e.getX(), e.getY());

                    if (h != null && !h.equalTo(mLastHighlighted)) {
                        mLastHighlighted = h;
                        mChart.highlightValue(h, true);
                    }
                    break;

            }
        }
        return super.onTouch(v, e);
    }

    @Override
    protected void performHighlight(Highlight h, MotionEvent e) {
        if (mChart.valuesToHighlight()) {
            h = null;
        }
        super.performHighlight(h, e);
    }
}
